import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from '@nestjs/typeorm'
import { ConfigService } from './config.service'
import { Injectable } from '@nestjs/common'
import { join } from 'path'
type DatabaseType = 'mysql'
'mariadb' ||
    'postgres' ||
    'cockroachdb' ||
    'sqlite' ||
    'mssql' ||
    'sap' ||
    'oracle' ||
    'cordova' ||
    'nativescript' ||
    'react-native' ||
    'sqljs' ||
    'mongodb' ||
    'aurora-data-api' ||
    'aurora-data-api-pg' ||
    'expo'
@Injectable()
export class TypeOrmConfigService implements TypeOrmOptionsFactory {
    // 注入config service取得env变量
    constructor(private readonly configService: ConfigService) {} // 就是回传TypeOrmOptions对象
    createTypeOrmOptions(): TypeOrmModuleOptions {
        return {
            type: this.configService.get('MYSQL_TYPE') as DatabaseType, //  as DatabaseType,
            host: this.configService.get('MYSQL_HOST'),
            port: this.configService.getNumber('MYSQL_PORT'),
            username: this.configService.get('MYSQL_USERNAME'),
            password: this.configService.get('MYSQL_PASSWORD'),
            database: this.configService.get('MYSQL_DATABASE'),
            synchronize: this.configService.getBoolean('MYSQL_SYNCHRONIZE'),
            logging: this.configService.getBoolean('MYSQL_LOGGING'),
            maxQueryExecutionTime: this.configService.getNumber(
                'MYSQL_MAXQUERY'
            ),
            entities: [join(__dirname, '../**/entity/*.{ts,js}')],
            timezone: 'UTC'
        }
    }
}
